package com.leapp.sk.sever.login

import android.text.TextUtils
import cc.vv.lkbasecomponent.util.LKSPUtil
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.util.KEY_PHONE
import com.example.util.ObjectProcessingUtil
import com.leapp.sk.bean.UserInfoEntity
import com.leapp.sk.bean.response.login.LoginResponseEntity
import com.example.util.finallist.*

/**
 * 用户信息逻辑处理类
 * @author  chaohaipeng  on 2018/8/16.
 */
class UserInfoSever {
    private object mHolder {
        val instance = UserInfoSever()
    }
    companion object {
        fun getInstance(): UserInfoSever {
            return mHolder.instance
        }
    }

    /**
     * 保存用户信息
     */
    fun saveData(obj: LoginResponseEntity?){
      val userInfoEntity:UserInfoEntity=ObjectProcessingUtil.objMandatoryConversionClass(obj?.data, UserInfoEntity::class.java)

        val token =if (null == userInfoEntity?.token) "" else userInfoEntity?.token
        val id =if (null == userInfoEntity?.id) "" else userInfoEntity?.id
        val avatar=if (null == userInfoEntity?.avatar) "" else userInfoEntity?.avatar
        val name=if (null == userInfoEntity?.name) "" else userInfoEntity?.name
        val nickName=if (null == userInfoEntity?.nickName) "" else userInfoEntity?.nickName
        val phone=if (null == userInfoEntity?.phone) "" else userInfoEntity?.phone
        val status=userInfoEntity?.status
        val type=userInfoEntity?.type
        val sex=userInfoEntity?.sex
        LKSPUtil.getInstance().put(KEY_TOKEN,token!!)
        LKSPUtil.getInstance().put(KEY_USERID,id!!)
        LKSPUtil.getInstance().put(KEY_AVATAR,avatar!!)
        LKSPUtil.getInstance().put(KEY_NAME,name!!)
        LKSPUtil.getInstance().put(KEY_STATUS,status)
        LKSPUtil.getInstance().put(KEY_NICKNAME,nickName!!)
        LKSPUtil.getInstance().put(KEY_STATUS,status)
        LKSPUtil.getInstance().put(KEY_CLASS_TYPE,type)
        LKSPUtil.getInstance().put(KEY_PHONE,phone!!)
        LKSPUtil.getInstance().put(KEY_SEX,sex)
        val accountTable = AccountDao.getInstance().queryById(userInfoEntity?.phone)
            if (accountTable==null||!TextUtils.equals(accountTable?.avatar,avatar)||!TextUtils.equals(accountTable?.nick,nickName)){
                accountTable?.avatar=avatar
                accountTable?.nick=nickName
                accountTable?.account=userInfoEntity?.phone
                AccountDao.getInstance().createOrUpdate(accountTable)
            }
    }



}